package com.linjian.togetherbirthday.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.adapter.base.recyler.RecyclerAdapter;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.linjian.togetherbirthday.contant.HttpContant;
import com.linjian.togetherbirthday.utils.HttpUtils;
import com.linjian.togetherbirthday.utils.NoticeEvent;
import com.linjian.togetherbirthday.utils.SharedPreferencesUtil;
import com.sdsmdg.tastytoast.TastyToast;
import com.yuntongxun.plugin.common.common.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegistManagerFragment extends BaseFragment implements XRecyclerView.LoadingListener, View.OnClickListener {

    private XRecyclerView mRecyclerView;
    private RecyclerAdapter mRecylerAdapter;
    private ImageView backIv;
    private TextView titleTv,rightTv;

    public static RegistManagerFragment newInstance(String info){
        RegistManagerFragment registManagerFragment = new RegistManagerFragment();
        Bundle bundle = new Bundle();
        bundle.putString("info",info);
        registManagerFragment.setArguments(bundle);
        return registManagerFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int setView() {
        return R.layout.fragment_regist_manager;
    }

    @Override
    protected void init(View view) {
            backIv = view.findViewById(R.id.layout_head_back_iv);
            titleTv = view.findViewById(R.id.layout_head_title_tv);
            rightTv = view.findViewById(R.id.layout_head_right_tv);
//        mRecyclerView = view.findViewById(R.id.regist_manager_xrv);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        mRecyclerView.setLayoutManager(layoutManager);
//
//        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
//        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
//        mRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);
//
//        mRecyclerView
//                .getDefaultRefreshHeaderView()
//                .setRefreshTimeVisible(true);
//        mRecyclerView.getDefaultFootView().setLoadingHint("自定义加载中提示");
//        mRecyclerView.getDefaultFootView().setNoMoreHint("自定义加载完毕提示");
//        mRecyclerView.setLimitNumberToCallLoadMore(1);
//        mRecyclerView.setLoadingListener(this);

//        mRecylerAdapter = new RecyclerAdapter<StoreListBean.DataBean.RecordsBean>(mItemListener) {
//            @Override
//            public ItemWrapper getItemHolder(int position) {
//                return new ItemWrapper(R.layout.item_regist_manager, RegisterMangerAdapter.class);
//            }
//        };
//        mRecyclerView.setAdapter(mRecylerAdapter);
    }


    @Override
    protected void initData(Bundle savedInstanceState) {
        httpGetCity(Integer.valueOf(getArguments().getString("info")));
        titleTv.setText("报名管理");
        rightTv.setVisibility(View.GONE);
        backIv.setOnClickListener(this);
    }


    private OnItemListener mItemListener = new OnItemListener() {
        @Override
        public void onItemClick(View view, int position,String data) {
        }

        @Override
        public void onItemAddress(View view, String address,String storeName) {
        }
    };


    /**
     * 请求报名人数
     */
    private void httpGetCity(int eventid) {
        HttpUtils.doGet(HttpContant.URL + HttpContant.QueryJoinMember + "?eventid=" + eventid
                , "SESSION=" + String.valueOf(SharedPreferencesUtil.get(getActivity(), "Cookie", ""))
                , new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        EventBus.getDefault().post(new NoticeEvent("requestFail"));
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        EventBus.getDefault().post(new NoticeEvent("getRegisterNum", response.body().string()));
                    }
                });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(NoticeEvent event) {
        switch (event.getCls()) {
            case "requestFail":
                TastyToast.makeText(getActivity(), "请求失败", TastyToast.LENGTH_SHORT, TastyToast.ERROR).show();
                break;
            case "getRegisterNum":
                LogUtil.e("getRegisterNum", event.getData().toString());
//                try {
//                    JSONObject object = new JSONObject(event.getData().toString());
//                    if (object.getInt("code") == 200){
//                        mList = new ArrayList<>();
//                        StoreListBean storeListBean = new Gson().fromJson(event.getData().toString(), StoreListBean.class);
//                        mList.addAll(storeListBean.getData().getRecords());
//                        mRecylerAdapter.addAll(mList);
//                        mRecylerAdapter.notifyDataSetChanged();
//                    }else {
//                        TastyToast.makeText(getActivity(),object.getString("msg"),TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }

                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
//        if (mRecyclerView != null) {
//            mRecyclerView.destroy();
//            mRecyclerView = null;
//        }
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_head_back_iv:
                getActivity().getSupportFragmentManager().popBackStack();
                break;
        }
    }

    interface OnItemListener {
        void onItemClick(View view, int position,String data);
        void onItemAddress(View view,String address,String storeName);
    }

//    static class RegisterMangerAdapter extends RecyclerHolder<StoreListBean.DataBean.RecordsBean> {
//
//
//        private OnItemListener mItemListener;
//        private ImageView storeImg;
//        private TextView nameTv, yearTv;
//        private Button button;
//
//        public RegisterMangerAdapter(View itemView, OnItemListener listener) {
//            super(itemView);
//
//            mItemListener = listener;
//
//            storeImg = itemView.findViewById(R.id.regist_user_img);
//            nameTv = itemView.findViewById(R.id.regist_user_name);
//            yearTv = itemView.findViewById(R.id.regist_user_year);
//            button = itemView.findViewById(R.id.regist_ok_bt);
//
//        }
//
//        @Override
//        public void onBindView(final StoreListBean.DataBean.RecordsBean dataBean) {
//            Glide.with(storeImg.getContext()).load(dataBean.getPhoto_urls()).asBitmap().error(R.mipmap.user_pic).placeholder(R.mipmap.user_pic).into(storeImg);
//            nameTv.setText(dataBean.getName());
//
//
//        }
//    }
}
